<?php

/**
 * 管理后台验证控制器
 * 
 * /Apps/Backend/Controller/AuthController.class.php
 * 
 * Zix @ 2015-06-08
 */

namespace Backend\Controller;

use Think\Controller;
use Common\Service\AuthService;

class AuthController extends Controller {

  var $session_name ; // session名称
  var $session_menu_name ;
  var $module; // 如Backend
  var $module_path; // 如backend

  function __construct() {
    parent::__construct();

    $this->session_name = C('SESSION_NAME');
    $this->session_menu_name = C('SESSION_MENU_NAME');
    $this->module = MODULE_NAME;
    $this->module_path = strtolower(MODULE_NAME);
  }

  public function index() {
    $this->display('login');
  }

  function login() {
    $post = I('post.');

    $this->auth = session($this->session_name);
    if (!empty($this->auth)) {
      redirect(U($this->module . '/index/index'));
    }

    if (empty($post)) {
      $data = array(
        'site_name' => C('SITE_NAME'),
        'config' => array(
          'remember_login_username' => cookie($this->session_name),
          'index_url' => U($this->module . '/index/index'),
          'do_login_url' => U($this->module . '/auth/login')
        )
      );

      $this->assign($data);
      $this->display('login');
    } else {
      $username = trim(I('post.username'));
      $password = trim(I('post.password'));
      $remember = trim(I('post.remember'));
      $Auth = AuthService::instance();
      $ret = $Auth->login_for_backend($username, $password, $remember, $this->session_name);
      $this->ajaxReturn($ret);
    }
  }

  function logout() {
    $Auth = AuthService::instance();
    $Auth->logout($this->session_name , $this->session_menu_name );
    redirect(U($this->module . '/auth/login'));
  }

}
